package bg.tu.varna.pms.client.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFileChooser;

import bg.tu.varna.pms.client.ApplicationContext;
import bg.tu.varna.pms.client.gui.MessagingDialog;
import bg.tu.varna.pms.client.gui.MessagingPanel;
import bg.tu.varna.pms.client.listener.MessagingListener;
import bg.tu.varna.pms.client.listener.UserEventImpl;
import bg.tu.varna.pms.model.ChatMessage;
import bg.tu.varna.pms.model.ChatRoom;
import bg.tu.varna.pms.model.FileMessage;
import bg.tu.varna.pms.model.communication.SyncMessage.OperationType;

public class MessagingController extends KeyAdapter implements
		MessagingListener, ActionListener {

	private MessagingDialog frame;
	private Map<MessagingPanel, ChatRoom> chatRoomMap = new HashMap<MessagingPanel, ChatRoom>();

	public MessagingController() {
		// this.panel = panel;
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			MessagingPanel panel = (MessagingPanel) e.getComponent()
					.getParent().getParent().getParent().getParent();
			
			sendMessage(panel);
		}
	}

	private void sendMessage(MessagingPanel panel) {

		ApplicationContext
				.getApplicationContext()
				.getUserContext()
				.sendChatMessage(chatRoomMap.get(panel),
						panel.getChatMessage());

		panel.clearTextField();
	}

	@Override
	public void chatRoomsModification(ChatRoom[] chatRooms) {
		// TODO Auto-generated method stub

	}

	@Override
	public void chatRoomModification(ChatRoom chatRoom, OperationType type) {
		// TODO Auto-generated method stub
		switch (type) {
		case Add:
			showChatRoom(chatRoom);
			break;
		case Remove:
			// TODO
			break;
		case Status:
			// TODO
			break;
		}

	}

	@Override
	public void messageReceived(ChatRoom chatRoom, ChatMessage message) {
		for (MessagingPanel panel : chatRoomMap.keySet()) {
			if (chatRoom.equals(chatRoomMap.get(panel))) {
				panel.showMessage(message.getMessage(),
						message.getSendDate());
			}
		}

	}

	@Override
	public void messageSend(ChatRoom chatRoom, ChatMessage message) {
		// TODO Auto-generated method stub

	}

	@Override
	public void messageDelivered(ChatRoom chatRoom, ChatMessage message) {
		// TODO Auto-generated method stub

	}

	@Override
	public void fileReceived(ChatRoom chatRoom, FileMessage file) {
		// TODO Auto-generated method stub

	}

	@Override
	public void fileSend(ChatRoom chatRoom, FileMessage file) {
		// TODO Auto-generated method stub

	}

	@Override
	public void fileDelivered(ChatRoom chatRoom, FileMessage file) {
		// TODO Auto-generated method stub

	}

	@Override
	public void fileProgress(ChatRoom chatRoom, FileMessage file, int progress) {
		// TODO Auto-generated method stub
	}

	private void showChatRoom(ChatRoom chatRoom) {
		if (frame == null) {
			frame = new MessagingDialog();
		}
		frame.setVisible(true);
		if (!chatRoomMap.containsKey(chatRoom)) {
			MessagingPanel panel = new MessagingPanel();
			panel.addController(this, this);
			frame.addTab(chatRoom.getUsernames(), panel);

			chatRoomMap.put(panel, chatRoom);
		}
	}

	// TODO
	public static void mockChat() {
		ChatRoom room = new ChatRoom("me", new String[] { "Username1" });
		// MessagingController c = new MessagingController();
		UserEventImpl.getInstance().addMessagingListener(c);
		c.showChatRoom(room);
	}

	private static final MessagingController c = new MessagingController();

	@Override
	public void actionPerformed(ActionEvent e) {
		switch (e.getActionCommand()) {
		case MypeActionCommand.SEND_FILE:
			final JFileChooser fc = new JFileChooser();
			int returnVal = fc.showOpenDialog(frame);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				ApplicationContext
						.getApplicationContext()
						.getUserContext()
						.sendFileMessage(chatRoomMap.get((MessagingPanel)((JButton) e.getSource()).getParent()),
								file.getAbsolutePath());
			}
			
			break;
		case MypeActionCommand.SEND_MESSAGE:
			MessagingPanel panel = ((MessagingPanel)((JButton)e.getSource()).getParent());
			sendMessage(panel);
			break;
		}
		
	}

}
